package com.example.demo.controller;

import com.example.demo.util.FileNameUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class FileUploadController {

    @RequestMapping(value = "/music/upload", method = RequestMethod.POST)
    @ResponseBody
    public Object uploadMusic(@RequestParam("file")MultipartFile file){
        Map<Object,Object> map = new HashMap<>();
        if(file.isEmpty()){
            map.put("code","1");
            map.put("msg","filename is empty");
        }
        else{
            String fileName = file.getOriginalFilename();
            if(!FileNameUtil.isAlphaNumber(fileName)){
                fileName = FileNameUtil.getRandomString() + ".mp3";
            }
            String filePath = System.getProperty("user.dir")+File.separator + "src"+ File.separator+"main"+File.separator+"resources"+File.separator+"static"+File.separator+"mp3"+File.separator;
            File dest = new File(filePath + fileName);
            try {
                dest.createNewFile();
                file.transferTo(dest);
                map.put("code","0");
                map.put("name",fileName);
            } catch (IOException e) {
                e.printStackTrace();
                map.put("code","2");
                map.put("msg","cannot upload"+dest.getPath());
            }
        }
        return map;
    }

    @RequestMapping(value = "/img/upload", method = RequestMethod.POST)
    @ResponseBody
    public Object uploadImg(@RequestParam("file")MultipartFile file){
        Map<Object,Object> map = new HashMap<>();
        if(file.isEmpty()){
            map.put("code","1");
            map.put("msg","filename is empty");
        }
        else{
            String fileName = file.getOriginalFilename();
            if(!FileNameUtil.isAlphaNumber(fileName)){
                fileName = FileNameUtil.getRandomString() + ".jpg";
            }
            String filePath = System.getProperty("user.dir")+File.separator+"src"+ File.separator+"main"+File.separator+"resources"+File.separator+"static"+File.separator+"img"+File.separator;
            File dest = new File(filePath+fileName);
            try {
                dest.createNewFile();
                file.transferTo(dest);
                map.put("code","0");
                map.put("name",fileName);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println(dest.getPath());
                map.put("code","2");
                map.put("msg","cannot upload"+dest.getPath());
            }
        }
        return map;
    }

    @RequestMapping(value = "/lyric/upload", method = RequestMethod.POST)
    @ResponseBody
    public Object uploadLyric(@RequestParam("file")MultipartFile file){
        Map<Object,Object> map = new HashMap<>();
        if(file.isEmpty()){
            map.put("code","1");
            map.put("msg","filename is empty");
        }
        else{
            String fileName = file.getOriginalFilename();
            if(!FileNameUtil.isAlphaNumber(fileName)){
                fileName = FileNameUtil.getRandomString() + ".jpg";
            }
            String filePath = System.getProperty("user.dir")+File.separator+"src"+ File.separator+"main"+File.separator+"resources"+File.separator+"static"+File.separator+"lyric"+File.separator;
            File dest = new File(filePath+fileName);
            try {
                dest.createNewFile();
                file.transferTo(dest);
                map.put("code","0");
                map.put("name",fileName);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println(dest.getPath());
                map.put("code","2");
                map.put("msg","cannot upload"+dest.getPath());
            }
        }
        return map;
    }
}
